﻿using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

namespace
{
    public class $table.EntityName$Dao
    {
        private static ConnectionStringSettings dbSettings = DataConnection.;
        private static string connectionString = dbSettings.ConnectionString;
        private static DbProviderFactory dbFactory = DbProviderFactories.GetFactory(dbSettings.ProviderName);
        
        public static List<$table.EntityName$> GetAll()
        {
            var list = new List<$table.EntityName$>();
            DbDataReader dr = null;
            try
            {
                dr = SqlHelper.ExecuteReader(dbFactory, connectionString
                    , CommandType.Text
                    , "select $allColumns:{$it.QualifiedColumnName$};separator=","$ from $table.QualifiedTableName$");
                while (dr != null && dr.Read())
                {
                    var obj = new $table.EntityName$();
                    $allColumns:{obj.$it.PropName$ = Field.Get$it.SystemTypeShortName$(dr, "$it.Name$");};separator="\r\n"$
                    list.Add(obj);
                }
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                    dr.Dispose();
                }
            }
            return list;
        }$if(table.IsTable)$
        
        public static $table.EntityName$ Get($pkColumns:{$it.SystemType$ $it.VarName$};separator=","$)
        {
            $table.EntityName$ obj = null;
            DbDataReader dr = null;
            try
            {
                var paras = new DbParameter[]{
                    $pkColumns:{SqlHelper.PrepareParameter(dbFactory, "$it.Name$", $it.DBType$, $it.VarName$)};separator=",\r\n"$
                };

                dr = SqlHelper.ExecuteReader(dbFactory, connectionString
                    , CommandType.Text
                    , "select $allColumns:{$it.QualifiedColumnName$};separator=","$ from $table.QualifiedTableName$ where $pkColumns:{$it.QualifiedColumnName$=@$it.PropName$};separator="and "$", paras);
                if (dr != null && dr.Read())
                {
                    obj = new $table.EntityName$();
                    $allColumns:{obj.$it.PropName$ = Field.Get$it.SystemTypeShortName$(dr, "$it.Name$");};separator="\r\n"$
                }
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                    dr.Dispose();
                }
            }
            return obj;
        }
        
        public static bool Insert($table.EntityName$ obj)
        {
            bool flag = false;
            try
            {
                var paras = new DbParameter[]{
                    $nonIdColumns:{SqlHelper.PrepareParameter(dbFactory, "$it.Name$", $it.DBType$, obj.$it.PropName$)};separator=",\r\n"$
                };
                SqlHelper.ExecuteNonQuery(dbFactory, connectionString
                    , CommandType.Text
                    , "insert into $table.QualifiedTableName$($nonIdColumns:{$it.QualifiedColumnName$};separator=", "$) values($nonIdColumns:{@$it.PropName$};separator=", "$)", paras);
                flag = true;
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }
        
        public static bool Update($table.EntityName$ obj)
        {
            bool flag = false;
            try
            {
                var paras = new DbParameter[]{
                    $allColumns:{SqlHelper.PrepareParameter(dbFactory, "$it.Name$", $it.DBType$, obj.$it.PropName$)};separator=",\r\n"$
                };
                SqlHelper.ExecuteNonQuery(dbFactory, connectionString
                    , CommandType.Text
                    , "update $table.QualifiedTableName$ set $updateColumns:{$it.QualifiedColumnName$=@$it.PropName$};separator=", "$ where $pkColumns:{$it.QualifiedColumnName$=@$it.PropName$};separator="and "$", paras);
                flag = true;
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }
        
        public static bool Delete($pkColumns:{$it.SystemType$ $it.VarName$};separator=","$)
        {
            bool flag = false;
            try
            {
                var paras = new DbParameter[]{
                    $pkColumns:{SqlHelper.PrepareParameter(dbFactory, "$it.Name$", $it.DBType$, $it.VarName$)};separator=",\r\n"$
                };
                SqlHelper.ExecuteNonQuery(dbFactory, connectionString
                    , CommandType.Text
                    , "delete from $table.QualifiedTableName$ where $pkColumns:{$it.QualifiedColumnName$=@$it.PropName$};separator="and "$", paras);
                flag = true;
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }$endif$
    }
}